package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    //套餐列表(查的是菜品 并且要把菜品的口味封装到菜品里面)
    @GetMapping("/dish/list")
    public ResultInfo list(Long categoryId, Integer status) {
        List<Dish> lsit = null;
        String dishKey = "dish_" + categoryId;
              //先判断缓存有没有东西  如果没有东西就差数据库 如果有缓存就查 redisTemplate
        if (redisTemplate.hasKey(dishKey)) {
            log.info("查询缓存");
            lsit= (List<Dish>) redisTemplate.opsForValue().get(dishKey);
        } else {
            log.info("查询数据库");
            lsit = dishService.list(categoryId, status);
            redisTemplate.opsForValue().set(dishKey, lsit);
        }


        return ResultInfo.success(lsit);

    }
}
